{Hint: save all files to location: \jni }
unit KaoqModule;

{$mode delphi}

interface

uses
  Classes, SysUtils, AndroidWidget, Laz_And_Controls, And_jni, preferences, lNet, ucommon, ubase64, fpjson, jsonparser;
  
type

  { TKaoqForm }

  TKaoqForm = class(jForm)
    jBtnCommit: jButton;
    jEdtUserid: jEditText;
    jLsvContent: jListView;
    jPanHeader: jPanel;
    jPreferens: jPreferences;
    jTxtUserid: jTextView;
    procedure jBtnCommitClick(Sender: TObject);
    procedure jLsvContentDrawItemTextColor(Sender: TObject; itemIndex: integer; itemCaption: string; out textColor: TARGBColorBridge);
    procedure KaoqFormJNIPrompt(Sender: TObject);
    procedure OnLTcpReceived(aSocket: TLSocket);
    procedure OnLTcpError(const msg: string; aSocket: TLSocket);
  private
    {private declarations}
  public
    {public declarations}
    lConn: TLTCP;
  end;

var
  KaoqForm: TKaoqForm;
  rcv : string;

implementation
  
{$R *.lfm}
  

{ TKaoqForm }

procedure TKaoqForm.KaoqFormJNIPrompt(Sender: TObject);
begin
  //jLsvContent.SetFontFromAssets('HANYI.TTF');
  jEdtUserid.Text := jPreferens.GetStringData('USERID','');
  lConn:= TLTCP(gPtrConn^);
  lConn.OnReceive := OnLTcpReceived;
  lConn.OnError   := OnLTcpError;
  rcv := '';
end;

procedure TKaoqForm.OnLTcpReceived(aSocket: TLSocket);
var
  s,ts,str: string;
  i,len: integer;
  j: TJSONObject;
  d: TJSONData;
  a: TJSONArray;
begin
  //接收信息
  lConn.GetMessage(s);
  rcv := rcv + s;
  len := Length(s);
  ts  := Copy(s, len-4, 5);
  if ('$END#' = ts) then
  begin
    rcv := Copy(rcv, 0, Length(rcv)-5);
    d := GetJSON(TFun.UnzipString(rcv));
    a := TJSONArray(d.FindPath('T'));
    rcv := '';
    jLsvContent.Clear;
    str := format('%-6s %-6s %-9s %-8s %-9s %-8s',['Date','Week','上班','夜班','异常','下班']);
    jLsvContent.Add(str);
    for i:=0 to a.Count -1 do
    begin
      j := a.Objects[i];
      str := format('%-6s %-6s %-6s %-6s %-6s %-6s',[j.Strings['DATE'],j.Strings['WEEK'],j.Strings['T2'],j.Strings['T3'],j.Strings['T4'],j.Strings['T5']]); //sl[0],sl[1],sl[3],sl[4],sl[5],sl[6]
      jLsvContent.Add(str);
    end;
  end;
end;

procedure TKaoqForm.OnLTcpError(const msg: string; aSocket: TLSocket);
begin
  ShowMessage(msg);
end;

procedure TKaoqForm.jBtnCommitClick(Sender: TObject);
var
  s, es, str : string;
  b : pbyte;
  i, n: dword;
  jsonObj, jsParam: TJSONObject;
begin
  jPreferens.SetStringData('USERID', jEdtUserid.Text);
    // 准备参数提交
  jsonObj := TJSONObject.Create;
  jsParam := TJSONObject.Create;
  jsonObj.Add('TIME',Now());
  jsonObj.Add('CMD','GetKaoqing');
  jsonObj.Add('PARM', jsParam);
  jsParam.Add('PMA',jEdtUserid.Text);
  // 加密
  s := jsonObj.AsJSON;
  es:= TFun.Encrypt(s);
  // BASE64
  i := length(es);
  n := (i div 3) * 4 + 5;
  Getmemory(b, n);
  FillByte(b^, n, 0);
  Base64Encode(PChar(es), i, b, n);
  if lConn.connected then
    lConn.Send(b^,n);
  Freemem(b);
end;

procedure TKaoqForm.jLsvContentDrawItemTextColor(Sender: TObject; itemIndex: integer; itemCaption: string; out textColor: TARGBColorBridge);
begin
  if 0=itemIndex then
    textColor := colbrNavy
  else if 0=(itemIndex mod 2) then
    textColor := colbrAqua;
end;



end.
